from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

import sys

sys.path.append(r'..')
from web_base.base_page import BasePage


class DiscuzPage(BasePage):
    url = r'http://192.168.253.129/upload/'
    # 登录定位
    username_loc = (By.ID, 'ls_username')
    password_loc = (By.ID, 'ls_password')
    login_btn_loc = (By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button/em')

    # 使用 super 进行引用
    def __init__(self, driver):
        super().__init__(driver)
        self.get_url(self.url)

    # 登录
    def login(self, username, password):
        self.ele_clear(self.username_loc)
        self.input_ele(self.username_loc, username)
        self.ele_clear(self.password_loc)
        self.input_ele(self.password_loc, password)
        self.ele_click(self.login_btn_loc)
        sleep(3)

    # 退出
    def logout(self):
        self.driver.find_element(By.LINK_TEXT, '退出').click()
        sleep(3)

    # 注册
    def register(self, username, password, pwd, email):
        self.driver.find_element(By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[4]/a').click()
        self.driver.find_element(By.ID, 'wy4l1L').send_keys(username)
        self.driver.find_element(By.ID, 'Kbtr87').send_keys(password)
        self.driver.find_element(By.ID, 'IKXahy').send_keys(pwd)
        self.driver.find_element(By.ID, 'Ike0N8').send_keys(email)
        self.driver.find_element(By.XPATH, '//*[@id="registerformsubmit"]/strong').click()
        sleep(6)

    # 看贴
    def read(self):
        self.driver.find_element(By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a').click()
        self.driver.find_element(By.CLASS_NAME, 's.xst').click()
        sleep(3)

    # 发帖
    def post(self, subject, message, username='test', password='123456'):
        self.login(username, password)
        # 发帖
        self.driver.find_element(By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a').click()
        self.driver.find_element(By.XPATH, '//*[@id="newspecial"]/img').click()
        self.driver.find_element(By.ID, 'subject').send_keys(subject)

        self.driver.switch_to.frame('e_iframe')  # 跳转到嵌套里面
        self.driver.find_element(By.XPATH, '/html/body').clear()
        self.driver.find_element(By.XPATH, '/html/body').send_keys(message)
        self.driver.switch_to.default_content()  # 跳出嵌套
        self.driver.find_element(By.ID, 'postsubmit').click()
        sleep(3)

        # 回帖

    # reply_click_loc = (By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a')
    # reply_cli_loc = (By.CLASS_NAME, 's.xst')
    # reply_loc = (By.ID, 'post_reply')
    # replys_loc = (By.ID, 'postmessage')
    # reply_tit_loc = (By.ID, 'postsubmit')

    def reply(self, tap, username='test', password='123456'):
        self.login(username, password)
        self.driver.find_element(By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a').click()  # 选择模块
        sleep(1)
        self.driver.find_element(By.CLASS_NAME, 's.xst').click()
        sleep(1)
        self.driver.find_element(By.ID, 'post_reply').click()  # 选择回复
        sleep(2)
        self.driver.find_element(By.ID, 'postmessage').send_keys(tap)
        sleep(1)
        self.driver.find_element(By.ID, 'postsubmit').click()
        sleep(2)


if __name__ == '__main__':
    username = 'test'
    password = '123456'
    tap = '1111111111'
    chrome_location = r'E:\test\chrome-win\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_location
    driver = webdriver.Chrome(options=options)

    discuz_page = DiscuzPage(driver)
    discuz_page.login(username, password)
    discuz_page.reply(tap)
    driver.quit()
